<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/6
 * Time: 15:18
 */

namespace Admin\Controller;


class GoodsController extends BaseController{
    /****************查询数据*********************/
    public function lst(){
        $model = D('Goods');
        //$data1 = $model->where(1)->select();
        $data = $model->search();
        //$this->assign('list',$data1);
        $this->assign($data);
        $catModel = D('Category');
        $catData = $catModel->getTree();
        $this->assign(array(
            'catData' => $catData,
            '_page_title' => '商品信息',
            '_page_btn_name' => '商品列表',
            '_module_name' => '商品模块',
            '_page_btn_link' => U('add'),
        ));
        $this->display();
    }
    /****************添加数据*********************/
    public function add(){
        if(IS_POST){

            set_time_limit(30);
            $pics = array();
            foreach ($_FILES['pic']['name'] as $k=>$v){
                $pics[] = array(
                    'name' => $v,
                    'type' => $_FILES['pic']['type'][$k],
                    'tmp_name' => $_FILES['pic']['tmp_name'][$k],
                    'error' => $_FILES['pic']['error'][$k],
                    'size' => $_FILES['pic']['size'][$k],
                );
            }
            $model = D('Goods');
            if($model->create(I('post.'),1)){ //var_dump(I('post.'));exit;
                if($model->add()){
                    $this->success('操作成功',U('lst'));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        } else {
            $brandModel = D('Brand');
            $brandData = $brandModel->select();

            $mlModel = D('member_level');
            $mlData  = $mlModel->select();

            $catModel = D('Category');
            $catData = $catModel->getTree();
            $this->assign(array(
                'mlData' => $mlData,
                'catData' => $catData,
                'brandData' => $brandData,
                '_page_title' => '商品添加',
                '_module_name' => '商品模块',
                '_page_btn_name' => '添加商品',
                '_page_btn_link' => U('add'),
            ));
            $this->display();
        }
    }
    /****************修改数据*********************/
    public function edit(){
        $id = I('get.id');
        $model = D('Goods');
        if(IS_POST){
            $model = D('Goods');
            if($model->create(I('post.'),2)){
                if(false !== $model->save()){
                     $this->success('操作成功',U('lst'));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        }
        $data = $model->find($id);
        $this->assign('data',$data);

        $brandModel = D('Brand');
        $brandData = $brandModel->select();

        $mlModel = D('member_level');
        $mlData = $mlModel->select();

        $mpModel = D('member_price');
        $mpData = $mpModel->where(array(
            'goods_id' => array('eq',$id),
        ))->select();
        $_mpData = array();
        foreach ($mpData as $k=>$v){
            $_mpData[$v['level_id']] = $v['price'];
        }
        /**取出相册现有图片**/
        $gpModel = D('goods_pic');
        $gpData = $gpModel->field('id,mid_pic')->where(array(
            'goods_id' => array('eq',$id),
        ))->select();

        /**取出所有分类做下拉框**/
        $catModel = D('Category');
        $catData = $catModel->getTree();
        /**取出分类id**/
        $gcModel = D('goods_cat');
        $gcData  = $gcModel->field('cat_id')->where(array(
            'goods_id' => array('eq',$id),
        ))->select();

        /**取出商品已有的属性值**/
        $attrModel = D('Attribute');
        $attrData = $attrModel->alias('a')
            ->field('a.id attr_id,a.attr_name,a.attr_type,a.attr_option_values,b.id,b.attr_value')
            ->join('LEFT JOIN __GOODS_ATTR__ b ON (a.id=b.attr_id AND b.goods_id='.$id.')')
            ->where(array(
                'a.type_id' => array('eq',$data['type_id']),
            ))->select();
        $this->assign(array(
            'catData' => $catData,
            'gcData' => $gcData,
            'gaData' => $attrData,
            'brandData' => $brandData,
            'mlData' => $mlData,
            'mpData' => $_mpData,
            'gpData' => $gpData,
            '_page_title' => '修改商品',
            '_page_btn_name' => '商品列表',
            '_module_name' => '商品模块',
            '_page_btn_link' => U('lst'),
        ));
        $this->display();
    }
    /****************删除数据*********************/
    public function del(){
        $model = D('Goods');
        if(false !== $model->delete(I('get.id'))){
            $this->success('删除成功！',U('lst'));
        } else {
            $this->error('删除失败' .$model->getError());
        }
    }

    public function ajaxDelPic(){
        $picId = I('get.picid');
        $gpModel = D('goods_pic');
        $pic = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->find($picId);
        deleteImage($pic);
        $gpModel->delete($picId);
    }

    public function ajaxGetAttr(){
        $typeId = I('get.type_id');
        $attrModel = D('Attribute');
        $attrData = $attrModel->where(array(
            'type_id' => array('eq',$typeId),
        ))->select();
        echo json_encode($attrData);
    }

    public function ajaxDelAttr(){
        $goodsId = addslashes(I('get.goods_id'));
        $gaid = addslashes(I('get.gaid'));
        $gaModel = D('goods_attr');
        $gaModel->delete($gaid);
        $gnModel = D('goods_number');
        $gnModel->where(array(
            'goods_id' => array('EXP',"=$goodsId AND FIND_IN_SET($gaid, goods_attr_id)"),
        ))->delete();
    }


    /****************商品库存量*********************/
    public function goods_number()
    {
        $id = I('get.id');
        $gnModel = D('goods_number');
        /**处理表单**/
        if (IS_POST) {
            //先删除原库存量
            $gnModel->where(array(
                'goods_id' => array('eq',$id),
            ))->delete();

            $gaid = I('post.goods_attr_id');
            $gn = I('post.goods_number');

            //计算商品属性ID和库存量的比例
            $gaidCount = count($gaid);
            $gnCount = count($gn);
            $rate = $gaidCount / $gnCount;

            //循环库存量
            $_i = 0; //取第几个商品属性ID
            foreach ($gn as $k => $v) {
                $_goodsAttrId = array();
                for ($i = 0; $i < $rate; $i++) {
                    $_goodsAttrId[] = $gaid[$_i];
                    $_i++;
                }
                //升序排序
                sort($_goodsAttrId,SORT_NUMERIC); //以数字形式排序
                $_goodsAttrId = (string)implode(',', $_goodsAttrId);
                $gnModel->add(array(
                    'goods_id' => $id,
                    'goods_attr_id' => $_goodsAttrId,
                    'goods_number' => $v,
                ));
            }
            $this->success('操作成功！',U('lst'));
            exit;
        }

        $gnModel = D('goods_number');
        $gnData = $gnModel->where(array(
            'goods_id' => $id,
        ))->select();
        $gaModel = D('goods_attr');
        $gaData = $gaModel->alias('a')
            ->field('a.*,b.attr_name')
            ->join('LEFT JOIN __ATTRIBUTE__ b ON a.attr_id=b.id')
            ->where(array(
                'a.goods_id' => array('eq' , $id),
                'b.attr_type' => array('eq' , '可选'),
            ))->select();
        $_gaData =array();
        foreach ($gaData as $k => $v){
            $_gaData[$v['attr_name']][] = $v;
        }
        $this->assign(array(
            'gaData' => $_gaData,
            'gnData' => $gnData,
        ));
        $this->assign(array(
            '_page_title' => '库存量',
            '_page_btn_name' => '返回列表',
            '_module_name' => '商品模块',
            '_page_btn_link' => U('lst'),
        ));

        $this->display();
    }

}